﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using YogaStudio.Data;
using YogaStudio.Data.Domain;

namespace YogaStudio.Service.BLL
{
    public class SectionService : YogaStudio.Service.BLL.ISectionService
    {
        private YogaStudioEntities db;

        public SectionService()
        {
            db = new YogaStudioEntities();
        }
        public List<Section> GetAllSections()
        {
            return db.Sections.ToList();
        }
        public Section GetSection(int id)
        {
            return db.Sections.Find(id);

        }
        public bool DeleteSection(Section section)
        {
            if (section == db.Sections.Remove(section))
            {
                db.SaveChanges();
            }
            return true;

        }
        public void UpdateSection(Section section)
        {
            db.Entry(section).State = EntityState.Modified;
            db.SaveChanges();
        }
        public void SaveSection(Section section)
        {
            db.Sections.Add(section);
            db.SaveChanges();
        }
        public IEnumerable<Section> GetSectionsOfNextSemester(int? facultyId)
        {
            return db.Sections.Include("Schedule.Semester").Include("Course").Include("Faculty").Include("Customers.Advisor")
                .Where(s => DateTime.Today < s.Schedule.FromDate)
                .Where(s => facultyId == null ? true : s.FacultyId == facultyId.Value);
        }
        public void Dispose(bool disposing)
        {
            if (disposing) db.Dispose();
        }

        public void Dispose()
        {
            Dispose(true);
        }
    }
}
